<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        :data="deps"
        :props="defaultProps"
        default-expand-all
        :expand-on-click-node="false"
      >
        <template v-slot="{ data }">
          <el-row type="flex" align="middle" style="width:100%;height=40px">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4" style="text-align:right">
              <span class="tree-manager">{{ data.managerName }}</span>
              <el-dropdown @command="depHandle($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
            <addList
              ref="zifrom"
              :show-dialog.sync="showDialog"
              :origin-depe="originDepe"
              :current-node-id="currentNodeId"
              @updataDeps="getDepartment"
            />
          </el-row>
        </template>
      </el-tree>
    </div>
  </div>
</template>

<script>
import { getDepartment, delDepartment } from '@/api/department'
import { transListToTree } from '@/utils'
import addList from './components/addList.vue'
export default {
  name: 'Department',
  components: {
    addList
  },
  data() {
    return {
      showDialog: false,
      originDepe: [],
      depts: {},
      deps: [],
      currentNodeId: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const res = await getDepartment()
      this.deps = transListToTree(res, 0)
      // console.log(this.deps)
      this.originDepe = res
    },
    depHandle(ccc, id) {
      if (ccc === 'add') {
        this.showDialog = true
        this.currentNodeId = id
        // console.log(id)
      } else if (ccc === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        // props跟新是异步的
        this.$nextTick(() => {
          this.$refs.zifrom.editlist()
        })
      } else {
        this.$confirm('你确定要删除该部门嘛', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(async() => {
          console.log(1)
          await delDepartment(id)
          this.$message.success('删除部门成功了哦')
          this.getDepartment()
        }).catch(() => {

        })
      }
    }

  }
}
</script>
<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
    width: 50px;
  display: inline-block;
  margin: 10px;
}

</style>
